#include <iostream>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int, int> E;
const static int NULLVAL = -1;
class Solution {
public:
    vector<int> Dijsktra(int n, vector<vector<E>> &graph, int start) {
        priority_queue<E, vector<E>, greater<E>> pq;
        vector<int> dis(n, NULLVAL);
        pq.emplace(dis[start] = 0, start);
        while (!pq.empty()) {
            auto [d, u] = pq.top(); pq.pop(); // c++17
            for (auto [v, w] : graph[u]) { // c++17
                if (dis[v] != NULLVAL && d + w >= dis[v]) { continue; }
                dis[v] = d + w;
                pq.emplace(dis[v], v);
            }
        }
        return dis;
    }
};

int main() {
    vector<vector<E>> graph; // first is w second is v
    int n, k, x, y;
    cin >> n >> k >> x >> y;
    graph.resize(n);
    while (k--) {
        int u, v, w;
        cin >> u >> v >> w;
        graph[u].push_back({ v,w });
        graph[v].push_back({ u,w });
    }
    cout << Solution().Dijsktra(n, graph, x)[y] << endl;
    return 0;
}